<?php
function create_random_string($random_length) {
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    $random_string = '';
	for ($i = 0; $i < $random_length; $i++) {
		$random_string .= $chars [mt_rand(0, strlen($chars) - 1)];
	}
	return $random_string;
}

function getDateTime($time = false){
	$time = $time ? $time : time();
	return date("Y-m-d H:i:s",$time);
}

function getZhDateTime($time = false){
	$time = $time ? $time : time();
	return date("Y年m月d日 H时i分s秒",$time);
}

function getMonthDay($time = false){
	$time = $time ? $time : time();
	return date("m-d",$time);
}

function getDates($time = false){
	$time = $time ? $time : time();
	return date("Y-m-d",$time);
}

function getZhDate($time = false){
	$time = $time ? $time : time();
	return date("Y年m月d日",$time);
}

function getTime($time = false){
	$time = $time ? $time : time();
	return date("H:i:s",$time);
}

function getIp(){
    static $realip;
    if (isset($_SERVER)){
        if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
            $realip = $_SERVER["HTTP_X_FORWARDED_FOR"];
        } else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
            $realip = $_SERVER["HTTP_CLIENT_IP"];
        } else {
            $realip = $_SERVER["REMOTE_ADDR"];
        }
    } else {
        if (getenv("HTTP_X_FORWARDED_FOR")){
            $realip = getenv("HTTP_X_FORWARDED_FOR");
        } else if (getenv("HTTP_CLIENT_IP")) {
            $realip = getenv("HTTP_CLIENT_IP");
        } else {
            $realip = getenv("REMOTE_ADDR");
        }
    }
    return $realip;
}

function serverIP(){
    return gethostbyname($_SERVER['SERVER_NAME']);
}

function GET($e=false,$def=""){
	if(!$e){
		$GET = array();
		foreach ($_GET as $key => &$value) {
			$GET[$key] = addslashes($value);
		}
		return $GET;
	}
	if(isset($_GET[$e])){
		return addslashes($_GET[$e]);
	}else{
		return $def;
	}
}

function POST($e=false,$def=""){
	if(!$e){
		$POST = array();
		foreach ($_POST as $key => &$value) {
			$POST[$key] = addslashes($value);
		}
		return $POST;
	}
	if(isset($_POST[$e])){
		return addslashes($_POST[$e]);
	}else{
		return $def;
	}
}

function I($e=false,$def=""){
	if(!$e){
		$REQUEST = array();
		foreach ($_REQUEST as $key => &$value) {
			$REQUEST[$key] = addslashes($value);
		}
		return $REQUEST;
	}
	if(isset($_REQUEST[$e])){
		return addslashes($_REQUEST[$e]);
	}else{
		return $def;
	}
}

//通过get方法获取int
function getInt($e,$def=""){
	if(isset($_GET[$e])){
		return (int)$_GET[$e];
	}else{
		return (int)$def;
	}
} 

//通过get方法获取int
function postInt($e,$def=""){
	if(isset($_POST[$e])){
		return (int)$_POST[$e];
	}else{
		return (int)$def;
	}
} 

function postStr($e,$def=""){
	if(isset($_POST[$e])){
		return $_POST[$e];
	}else{
		return $def;
	}
} 

//存到数据库前保护
function html2code($e){
	return htmlspecialchars($e,ENT_QUOTES);
}

function getCategoryNameById($id){
	$category_name = M("category")->where("id","=",$id)->find();
	return $category_name['name'];
}

function getNickNameById($id){
	$user_name = M("user")->where("id","=",$id)->find();
	return $user_name['nickname'];
}

function showIndexTitle($name,$id){
	return  '<div class="panel panel-default"><div class="panel-heading blue-panel-heading"><span class="lanmu-head">' . $name . '</span><a href="category.php?id=' . $id . '" title="' . $name . '" target="_blank" class="lanmu-head pull-right"> 更多 </a> </div><table class="table">';
}

function showIndexRow($data){
	if($data){
      foreach ($data as $value) {
         // $short = mb_substr($value['title'],0,18,'utf-8');
         $short = mb_strimwidth($value['title'],0,36,"",'utf-8');
         if( $short != $value['title'] )
            $short .= "...";
         echo "<tr><td><a class=\"lqx-panel-a\" href=\"article.php?id=" . $value['id'] . "\" target=\"_blank\" title=\"" . $value['title'] . "\">★ &nbsp;" . $short . "</a></td>
         <td align=\"right\">". getMonthDay($value['audit_time']) ."</td></tr>";
      }
   }
}

function IsAdminByUsername($username){
	$map = [];
	$map['username'] = $username;
	$map['is_admin'] = 1;
	return M("user")->where($map)->find();
}